package com.deke.home.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deke.entity.TbZone;
import com.deke.home.dao.TbZoneDao;
import com.deke.home.service.TbZoneService;
import com.deke.home.vo.TbZoneVo;
import com.deke.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @author ：ludi（206913808@qq.com）
 * @date ：Created in 2022/12/5 19:19
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class TbZoneServiceImpl implements TbZoneService {

    @Resource
    private TbZoneDao tbZoneDao;


    /**
     * @create by: ludi（206913808@qq.com）
     * @description: 分页查询
     * @create time: 2022/12/5 21:09
     * @param current
     * @param pageSize
     * @param tbZoneVo
     * @return com.deke.vo.Result<com.baomidou.mybatisplus.core.metadata.IPage<com.deke.entity.TbZone>>
     */
    @Override
    public Result<IPage<TbZone>> queryByConditionPage(Integer current, Integer pageSize, TbZoneVo tbZoneVo) {
        IPage<TbZone> page = new Page(current,pageSize);
        QueryWrapper<TbZone> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        if (StringUtils.hasText(tbZoneVo.getId())){
            wrapper.like("id",tbZoneVo.getId());
        }
        if (StringUtils.hasText(tbZoneVo.getZoneName())){
            wrapper.like("zone_name",tbZoneVo.getZoneName());
        }
        IPage<TbZone> iPage = tbZoneDao.selectPage(page, wrapper);
        return new Result<>(2000,"查询成功",iPage);
    }


    /**
     * @create by: ludi（206913808@qq.com）
     * @description: 删除
     * @create time: 2022/12/5 22:19
     * @param id
     * @return void
     */
    @Override
    public void deleteZone(TbZone tbZone,String id) {
        UpdateWrapper<TbZone> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        tbZoneDao.update(tbZone,wrapper);
    }



    /**
     * @create by: ludi（206913808@qq.com）
     * @description: 修改
     * @create time: 2022/12/5 22:19
     * @param tbZone
     * @return void
     */
    @Override
    public void updateZone(TbZone tbZone) {
        UpdateWrapper wrapper = new UpdateWrapper<>();
        wrapper.eq("id", tbZone.getId());
        tbZoneDao.update(tbZone,wrapper);
    }


    /**
     * @create by: ludi（206913808@qq.com）
     * @description: 添加
     * @create time: 2022/12/5 22:19
     * @param tbZone
     * @return void
     */
    @Override
    public void addZone(TbZone tbZone) {
        int i = RandomUtil.randomInt();
        if (i>0){
            tbZone.setId(Convert.toStr(i));
            tbZone.setIsDeleted("0");
            tbZoneDao.insert(tbZone);
        }else {
            int j = Math.negateExact(i);
            tbZone.setId(Convert.toStr(j));
            tbZone.setIsDeleted("0");
            tbZoneDao.insert(tbZone);
        }
    }


    /**
     * @create by: ludi（206913808@qq.com）
     * @description: 改变状态
     * @create time: 2022/12/5 23:34
     * @param tbZone
     * @param idDisable
     * @return void
     */
    @Override
    public void changeStatus(TbZone tbZone,Integer idDisable) {
        UpdateWrapper<TbZone> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", tbZone.getId());
        tbZoneDao.update(tbZone,wrapper);
    }
}